//
//  PropertyPickCell.m
//  Trackit
//
//  Created by yaodaobin on 2017/6/30.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "PropertyPickCell.h"
#import "DatePickerView.h"

@interface PropertyPickCell()

@property (nonatomic, strong) DatePickerView *pickerView;

@end

@implementation PropertyPickCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initUI];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    kWeakSelf

    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.userInteractionEnabled = YES;
    self.contentLabel.text = @"请选择";
    self.contentLabel.font = self.titleLabel.font;
    self.contentLabel.textColor = self.titleLabel.textColor;
    [self.contentView addSubview:self.contentLabel];
    self.contentLabel.tappedBlock = ^(UITapGestureRecognizer *tap) {
        [weakSelf showPicker];
    };
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(20);
        make.centerY.offset(0);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.leading.equalTo(weakSelf.titleLabel.mas_trailing).offset(20);
        make.trailing.equalTo(weakSelf.mas_trailing).offset(-20);
    }];
    
    //设置label1的content hugging 为1000
    [self.titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    //设置label1的content compression 为1000
    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    //设置右边的label2的content hugging 为1000
    [self.contentLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    
    //设置右边的label2的content compression 为250
    [self.contentLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    
}

- (void)setCellTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (void)resignDatePicker {
    if (self.pickerView) {
        [self.pickerView dismiss];
    }
    if (self.locateView) {
        [self.locateView dismiss];
    }
}

- (void)setItem:(PropertyItem *)item {
    _item = item;
    self.titleLabel.text = item.inputTitle;
    self.pickDate = [item.inputTitle isEqualToString:@"录入日期"];
    if (self.pickDate) {
        if (item.itemValue.length == 0) {
            NSString *dString = [CommonFunction stringFromDate:[NSDate date] format:@"yyyy-MM-dd"];
            item.itemValue = dString;
            self.contentLabel.text = dString;
            self.contentLabel.textColor = [UIColor blackColor];
        }
    } else {
        if ([self.taskDetail.locationInfo hasData]) {
            NSString *str = @"";
            if ([self.titleLabel.text isEqualToString:@"州县乡"]) {
                str = [NSString stringWithFormat:@"%@ %@ %@", self.taskDetail.locationInfo.province ? : @"",self.taskDetail.locationInfo.city ? : @"",self.taskDetail.locationInfo.town ? : @""];
            } else if ([self.titleLabel.text isEqualToString:@"村或合作社"]) {
                str = self.taskDetail.locationInfo.village;
            }
            self.contentLabel.text = str;
            self.contentLabel.textColor = [UIColor blackColor];
        }

    }
}

- (void)showPicker {
    if (self.resignKeyboardBlock) {
        self.resignKeyboardBlock();
    }
    if (self.pickDate) {
        if (!self.pickerView) {
            self.pickerView = [[DatePickerView alloc] initWithTitle:@"" delegate:self];
        }
        AppDelegate *appDelegate = [AppDelegate sharedAppDelegate];
        [self.pickerView showInView:appDelegate.window];
    } else {
        if (!self.locateView) {
            self.locateView = [[TSLocateView alloc] initWithTitle:@"选择地区" delegate:self isVillage:[self.titleLabel.text isEqualToString:@"村或合作社"]];
        }
        [self.locateView.locatePicker reloadAllComponents];
        AppDelegate *appDelegate = [AppDelegate sharedAppDelegate];
        [self.locateView showInView:appDelegate.window];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([actionSheet isKindOfClass:[DatePickerView class]]) {
        NSString *dString = [CommonFunction stringFromDate:self.pickerView.date format:@"yyyy-MM-dd"];
        self.contentLabel.text = dString;
        self.item.itemValue = dString;
        self.contentLabel.textColor = [UIColor blackColor];
    } else if ([actionSheet isKindOfClass:[TSLocateView class]]) {
        if (buttonIndex == 1) {
            self.locateView = (TSLocateView *)actionSheet;
            TSLocation *location = self.locateView.locate;
            self.taskDetail.locationInfo = location;
            self.contentLabel.textColor = [UIColor blackColor];
            if (self.selectedAreaBlock) {
                self.selectedAreaBlock();
            }
        }
    }
}


@end
